/**
 * 
 */
package com.contact.util.util.filters;

import com.contact.util.StringUtils;

public enum SearchType {
    CONTAINS("contains"),
    STARTS_WITH("startsWith"),
    ENDS_WITH("endsWith");

    private final static String WILDCARD = "%";
    private String code;

    SearchType(String code) {
        setCode(code);
    }

    public String getCode() {
        return code;
    }

    private void setCode(String code) {
        this.code = code;
    }

    public static SearchType getByCode(String code) {

        for (SearchType st : values()) {
            if (st.getCode().equals(code)) {
                return st;
            }
        }

        return null;
    }

    public String getWildCardValue(String value) {

        String escValue = value; //TODO escape %

        if (StringUtils.isEmpty(escValue)) {
            return WILDCARD;
        }

        StringBuilder sb = new StringBuilder();

        if (this.equals(CONTAINS) || this.equals(ENDS_WITH)) {
            sb.append(WILDCARD);
        }

        sb.append(escValue);

        if (this.equals(CONTAINS) || this.equals(STARTS_WITH)) {
            sb.append(WILDCARD);
        }

        return sb.toString();
    }

}